"""
Provides an entry point for a build script template, which includes:
* Support for the common 'configure, build, install' flow
* Support for standard configs: --mode=debug|release --prefix

Sample usage:

build.py
  
  PROJECT = 'my_project'

  def configure(bld):
    # configure command implementation
    
  def build(bld):
    # build command implementation
    
  def install(bld):
    # install command implementation
  
  byscript = bylib.Script(PROJECT)
  byscript.add_commands(
      configure=configure,
      build=build,
      install=install)
  byscript.add_common_args()
  byscript.run()
"""

from argparse import ArgumentParser

import build

class Script(object):
  
  def __init__(self, project_name):
    self.args_parser = ArgumentParser(project_name)
    self.commands = {}
    
  def add_commands(self, **kwargs):
    self.commands.update(kwargs)
    
  def add_common_args(self):
    self.args_parser.add_argument(
        '--mode',
        choices=('release', 'debug'),
        default='release',
        nargs='?',
        help = 'Selects the build configuration.')
    self.args_parser.add_argument(
        '--output',
        help = 'Build output directory.')
    self.args_parser.add_argument(
        '--prefix',
        help = 'Prefix')

  def run(self):
    command_group = self.args_parser.add_argument_group('command')
    command_group.add_argument(
        'command',
        choices=self.commands.keys(),
        help='Specifies the build action to perform.')
    args = self.args_parser.parse_args()
    bld = build.Build()
    bld.output = args.output
    bld.args = args
    self.commands[args.command](bld)
